package com.ocean.web.controller.product;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ocean.common.annotation.Log;
import com.ocean.common.enums.BusinessType;
import com.ocean.productDict.domain.OProductType;
import com.ocean.productDict.service.IOProductTypeService;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.utils.poi.ExcelUtil;
import com.ocean.common.core.page.TableDataInfo;

/**
 * 产品类型Controller
 *
 * @author ocean
 * @date 2022-12-29
 */
@Controller
@RequestMapping("/product/productType")
public class OProductTypeController extends BaseController
{
    private String prefix = "product/productType";

    @Autowired
    private IOProductTypeService oProductTypeService;

    //@RequiresPermissions("product:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/list";
    }

    /**
     * 查询产品类型列表
     */
    //@RequiresPermissions("product:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OProductType oProductType)
    {
        startPage();
        List<OProductType> list = oProductTypeService.selectOProductTypeList(oProductType);
        return getDataTable(list);
    }

    /**
     * 导出产品类型列表
     */
    //@RequiresPermissions("product:type:export")
    @Log(title = "产品类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OProductType oProductType)
    {
        List<OProductType> list = oProductTypeService.selectOProductTypeList(oProductType);
        ExcelUtil<OProductType> util = new ExcelUtil<OProductType>(OProductType.class);
        return util.exportExcel(list, "产品类型数据");
    }

    /**
     * 新增产品类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品类型
     */
    //@RequiresPermissions("product:type:add")
    @Log(title = "产品类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OProductType oProductType)
    {
        try {
            int i = oProductTypeService.insertOProductType(oProductType);
            if (i==-3){
                return AjaxResult.warn("类型已存在");
            }else{
                return toAjax(i);
            }
        }catch (Exception e){
            logger.error("保存产品类型失败",e);
            return AjaxResult.error("保存产品类型失败");
        }
    }

    /**
     * 修改产品类型
     */
    //@RequiresPermissions("product:type:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        OProductType oProductType = oProductTypeService.selectOProductTypeById(id);
        mmap.put("oProductType", oProductType);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品类型
     */
    //@RequiresPermissions("product:type:edit")
    @Log(title = "产品类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OProductType oProductType)
    {
        try {
            return toAjax(oProductTypeService.updateOProductType(oProductType));
        }catch (Exception e){
            logger.error("修改产品类型失败",e);
            return AjaxResult.error("修改产品类型失败");
        }
    }

    /**
     * 删除产品类型
     */
    //@RequiresPermissions("product:type:remove")
    @Log(title = "产品类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try {
            return toAjax(oProductTypeService.deleteOProductTypeByIds(ids));
        }catch (Exception e){
            logger.error("删除产品类型失败",e);
            return AjaxResult.error("删除产品类型失败");
        }
    }

    /**
     * 查看产品类型详情
     */
    //@RequiresPermissions("product:type:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
        OProductType oProductType = oProductTypeService.selectOProductTypeById(id);
        mmap.put("oProductType", oProductType);
        return prefix + "/detail";
    }

}
